﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Tags;
using System.Net;
using System.IO;

namespace ASPNETX
{
    public class Seeker
    {
        /// <summary>
        /// 主页
        /// </summary>
        public string RootURL { get; set; }

        /// <summary>
        /// 检索到的URL列表
        /// </summary>
        private List<string> URLList;

        public Seeker()
        {
            URLList = new List<string>();
        }

        /// <summary>
        /// 检索主页中相关的url地址
        /// </summary>
        /// <param name="depth">检索的深度</param>
        /// <returns></returns>
        public List<string> GetURLs(int depth)
        {
            List<string> result = new List<string>();
            
            string strContent = HTMLHelper.GetURLContent(RootURL);

            Parser parser = Parser.CreateParser(strContent, "GBK");
            HtmlPage page = new HtmlPage(parser);

            try
            {
                parser.VisitAllNodesWith(page);
            }
            catch (ParserException e1)
            {
                //MessageBox.Show(e1.Message);
            }

            NodeList nodelist = page.Body;
            NodeFilter filter = new TagNameFilter("A");
            nodelist = nodelist.ExtractAllNodesThatMatch(filter, true);

            for (int i = 0; i < nodelist.Size(); i++)
            {
                ATag link = (ATag)nodelist.ElementAt(i);

                string currentURL = link.GetAttribute("href");

                //首先判断地址不能为空
                if (!string.IsNullOrEmpty(currentURL))
                {
                    //处理地址是以"/"开头的情况,比如"/newscontent.htm"
                    if (currentURL.StartsWith("/"))
                    {
                        currentURL = RootURL + currentURL;
                    }
                    //当其不以"/"开头的情况，确保,比如"http://www.abc.com","newscontent.htm",前目录"../newscontent.htm"在这里不会存在
                    else if (!currentURL.Contains(RootURL))
                    {
                        //如果url是以http开头
                        if (currentURL.StartsWith("http"))
                        {
                            continue;
                        }
                        else
                        {
                            currentURL = RootURL + "/" + currentURL;
                        }
                    }
                }

                //如果当前地址在地址列表中已经存在则不把其加入地址列表
                if (URLList.Contains(currentURL))
                {
                    continue;
                }

                if (depth > 1)
                {
                    foreach (string item in URLList)
                    { 
                        
                    }
                }

                result.Add(currentURL);
                URLList.Add(currentURL);
            }

            return result;
        }

        /// <summary>
        /// 根据指定的一个子页面获得其所有相关的url
        /// </summary>
        /// <param name="url"></param>
        private void SeekSubURLs(string url)
        {
            string strContent = HTMLHelper.GetURLContent(url);

            Parser parser = Parser.CreateParser(strContent, "GBK");
            HtmlPage page = new HtmlPage(parser);

            try
            {
                parser.VisitAllNodesWith(page);
            }
            catch (ParserException e1)
            {
                //MessageBox.Show(e1.Message);
            }

            NodeList nodelist = page.Body;
            NodeFilter filter = new TagNameFilter("A");
            nodelist = nodelist.ExtractAllNodesThatMatch(filter, true);

            for (int i = 0; i < nodelist.Size(); i++)
            {
                ATag link = (ATag)nodelist.ElementAt(i);

                string currentURL = link.GetAttribute("href");

                //首先判断地址不能为空
                if (!string.IsNullOrEmpty(currentURL))
                {
                    //处理地址是以"/"开头的情况,比如"/newscontent.htm"
                    if (currentURL.StartsWith("/"))
                    {
                        currentURL = RootURL + currentURL;
                    }
                    //当其不以"/"开头的情况，确保,比如"http://www.abc.com","newscontent.htm",前目录"../newscontent.htm"的情况不考虑
                    else if (!currentURL.Contains(RootURL))
                    {
                        //如果url是以http开头
                        if (currentURL.StartsWith("http"))
                        {
                            continue;
                        }
                        else
                        {
                            currentURL = RootURL + "/" + currentURL;
                        }
                    }
                }

                //如果当前地址在地址列表中已经存在则不把其加入地址列表
                if (URLList.Contains(currentURL))
                {
                    continue;
                }

                URLList.Add(currentURL);
            }
        }
    }
}
